package com.homework1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CompressFile implements Runnable {
    
    private String fileName;
    
    

    public CompressFile(String fileName) {
	super();
	this.fileName = fileName;
    }



    @Override
    public void run() {
	
	byte[] buffer = new byte[1024];
	FileInputStream fis = null;
	
	try {
	    FileOutputStream fos = new FileOutputStream("file.zip");
	    ZipOutputStream zos = new ZipOutputStream(fos);
	    ZipEntry ze = new ZipEntry(fileName);
	    zos.putNextEntry(ze);
	    fis = new FileInputStream(fileName);
	    
	    int len;
	    while((len=fis.read(buffer))>0){
		zos.write(buffer, 0, len);
	    }
	    
	    
	} catch (FileNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} finally{
	    try {
		fis.close();
	    } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	}
	
	
    }

}
